package com.wyzz.hr.base.wf.leaverequest;

import java.math.BigDecimal;
import java.util.*;
import org.eclipse.birt.report.model.api.util.StringUtil;

import com.wyzz.hr.base.wf.AbstractWfConfigAction;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.WorkFlowApprover;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.vo.WorkFlowApproverBean;

public class ExaminWfConfigAction extends AbstractWfConfigAction {

	public ExaminWfConfigAction() {
	}

	public List formatList(Position position, List list) {
		List result = new ArrayList();
		if (list == null || list.isEmpty()) {
			WorkFlowApproverBean bean = new WorkFlowApproverBean();
			bean = newWorkFlowApproverBean(position, "leaverequest");
			bean.setWorkFlow(newWorkFlowApprover(position, "leaverequest"));
			result.add(bean);
			bean = newWorkFlowApproverBean(position, "overtimerequest");
			bean.setWorkFlow(newWorkFlowApprover(position, "overtimerequest"));
			result.add(bean);
		} else {
			Map map = new HashMap();
			WorkFlowApprover wf;
			for (Iterator it = list.iterator(); it.hasNext(); map.put(wf
					.getWorkFlowApproverType(), wf))
				wf = (WorkFlowApprover) it.next();

			wf = (WorkFlowApprover) map.get("leaverequest");
			WorkFlowApproverBean bean = new WorkFlowApproverBean();
			if (wf == null) {
				bean = newWorkFlowApproverBean(position, "leaverequest");
				bean.setWorkFlow(newWorkFlowApprover(position, "leaverequest"));
			} else {
				bean = newWorkFlowApproverBean(position, "leaverequest");
				if (wf.getWorkFlowLimit().doubleValue() == 0.0D
						&& wf.getWorkFlowApproverInd().intValue() != 0)
					wf.setWorkFlowApproverInd(Integer.valueOf(3));
				bean.setWorkFlow(wf);
			}
			result.add(bean);
			wf = (WorkFlowApprover) map.get("overtimerequest");
			if (wf == null) {
				bean = newWorkFlowApproverBean(position, "overtimerequest");
				bean.setWorkFlow(newWorkFlowApprover(position,
						"overtimerequest"));
			} else {
				bean = newWorkFlowApproverBean(position, "overtimerequest");
				if (wf.getWorkFlowLimit().doubleValue() == 0.0D
						&& wf.getWorkFlowApproverInd().intValue() != 0)
					wf.setWorkFlowApproverInd(Integer.valueOf(3));
				bean.setWorkFlow(wf);
			}
			result.add(bean);
		}
		return result;
	}

	public String update() {
		String lrLimitErrorMsg = "\u8BF7\u5047\u6709\u6761\u4EF6\u7EC8\u5BA1\u65F6\u5BA1\u6279\u989D\u5EA6\u4E0D\u80FD\u4E3A\u7A7A\uFF01";
		String orLimitErrorMsg = "\u52A0\u73ED\u6709\u6761\u4EF6\u7EC8\u5BA1\u65F6\u5BA1\u6279\u989D\u5EA6\u4E0D\u80FD\u4E3A\u7A7A\uFF01";
		workFlow1.setWorkFlowApproverId(positionId);
		if (workFlow1.getWorkFlowApproverInd().intValue() == 1
				&& (workFlow1.getWorkFlowLimit() == null || workFlow1
						.getWorkFlowLimit().doubleValue() == 0.0D)) {
			addActionError(lrLimitErrorMsg);
			return "success";
		}
		if (workFlow1.getWorkFlowApproverInd().intValue() == 3)
			workFlow1.setWorkFlowApproverInd(Integer.valueOf(1));
		if (StringUtil.isEmpty(workFlow1.getId())) {
			if (workFlow1.getWorkFlowApproverInd().intValue() != 2)
				workFlowApproverBo.addWorkFlowApprover(workFlow1);
		} else if (workFlow1.getWorkFlowApproverInd().intValue() == 2)
			workFlowApproverBo.deleteWorkFlowApproverById(workFlow1.getId());
		else
			workFlowApproverBo.updateWorkFlowApprover(workFlow1);
		workFlow2.setWorkFlowApproverId(positionId);
		if (workFlow2.getWorkFlowApproverInd().intValue() == 1
				&& (workFlow2.getWorkFlowLimit() == null || workFlow2
						.getWorkFlowLimit().doubleValue() == 0.0D)) {
			addActionError(orLimitErrorMsg);
			return "success";
		}
		if (workFlow2.getWorkFlowApproverInd().intValue() == 3)
			workFlow2.setWorkFlowApproverInd(Integer.valueOf(1));
		if (StringUtil.isEmpty(workFlow2.getId())) {
			if (workFlow2.getWorkFlowApproverInd().intValue() != 2)
				workFlowApproverBo.addWorkFlowApprover(workFlow2);
		} else if (workFlow2.getWorkFlowApproverInd().intValue() == 2)
			workFlowApproverBo.deleteWorkFlowApproverById(workFlow2.getId());
		else
			workFlowApproverBo.updateWorkFlowApprover(workFlow2);
		return "success";
	}

	public String workFlowList() {
		IPositionBo positionBO = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Position position = positionBO.getPositionById(positionId);
		String type[] = { "overtimerequest", "leaverequest" };
		List list = workFlowApproverBo.getWorkFlowApprover(position.getId(),
				type);
		items = formatList(position, list);
		return "success";
	}

	public WorkFlowApprover getWorkFlow1() {
		return workFlow1;
	}

	public void setWorkFlow1(WorkFlowApprover workFlow1) {
		this.workFlow1 = workFlow1;
	}

	public WorkFlowApprover getWorkFlow2() {
		return workFlow2;
	}

	public void setWorkFlow2(WorkFlowApprover workFlow2) {
		this.workFlow2 = workFlow2;
	}

	private WorkFlowApprover workFlow1;
	private WorkFlowApprover workFlow2;
}
